<?php

ini_alter("max_execution_time","3600");
ini_alter("default_socket_timeout","3600"); 

include_once(APPLICATION_ROOT."/web_config.php");
include_once(APPLICATION_ROOT."/config.php");
require_once(APPLICATION_ROOT."/oracleaccess/classes/DbConnect.php" );
require_once(APPLICATION_ROOT."/logs/LogApp.class" );

//include(APPLICATION_ROOT."/scadenzario/transfmese.php");

$connessione = new DbConnect();//connessione viste locale

$data_abbinamento=date('Ymd');

 $select_recs="SELECT CIG,PROG_CUI,ID_SIMOG,SCHEDA,DATA_ID,DENOM_STAZIONE_APPALTANTE,CF_RUP,ID_SCHEDA_LOCALE,CF_UTENTE,ID FROM AVCP WHERE SCHEDA='DATI_COMUNI' AND ID_SCHEDA_LOCALE IS NULL order by cig";
 $query_recs = $connessione -> doquery_oracle($select_recs, 'select');
 
 
 for($r=1; $r<=(count($query_recs)); $r++){
  $cig_xls=$prog_cui_xls=$id_simog_xls=$scheda_db=$data_id_xls=$resultUDC = "";
  $cig_xls=     $query_recs[$r][1];
  $prog_cui_xls=$query_recs[$r][2];
  $id_simog_xls=$query_recs[$r][3];
  $scheda_db=   $query_recs[$r][4];
  $data_id_xls= $query_recs[$r][5]; 
  
  //SELECT ID SCHEDA LOCALE
  $sel_DC="SELECT ID FROM  DATI_COMUNI WHERE cig='".$cig_xls."' ";
  $rs_DC = $connessione->doquery_oracle($sel_DC, 'select');
  if($rs_DC[1][1]!=""){
   $id_scheda_locale=$rs_DC[1][1];
   $updateDC="update DATI_COMUNI set ID_SIMOG='".$id_simog_xls."', SCHEDA_CHIUSA='I', ARCHIVIATA='S' WHERE id='".$id_scheda_locale."' ";
   $connessione->doquery_oracle($updateDC, 'update');  

   $update_AVCP="update AVCP set ID_SCHEDA_LOCALE='".$id_scheda_locale."',DATA_ABBINAMENTO='".$data_abbinamento."' WHERE CIG='".$cig_xls."' AND SCHEDA='DATI_COMUNI' ";
   $connessione->doquery_oracle($update_AVCP, 'update');  
   
  }else{
   $message["SCHEDA"]="DATI_COMUNI";
   $message["CIG"]=$cig_xls;
   $message["NUM_PROGR"]="";
   $message["NUM_PROGR_SCHEDA"]="";
   $message["OPERAZIONE"]=$sel_esist_cig; 
   $message["MSG"]="Update fallita: non esiste il CIG.";
   LogApp::getInstance()->LogWrite($message);   
  }
 }

echo("<br>END DATI_COMUNI");



?>